package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionData;
import cn.wolfcode.mapper.ExamMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.service.IQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private IQuestionService questionService;

    @Override
    public void save(Exam exam) {
        examMapper.insert(exam);
        Long examId = exam.getId();
        List<Integer> single = questionService.selectSingleType(exam.getSingleNumber());
        List<Integer> multi = questionService.selectMultiType(exam.getMultiNumber());
        List<Integer> judge = questionService.selectJudgeType(exam.getJudgeNumber());
        int k = 1;
        for (int i = 0; i < single.size(); i++) {
            Integer singleId = single.get(i);
            questionService.insertSingleRelation(examId, singleId, k, exam.getSingleScore());
            k++;
        }

        for (int i = 0; i < multi.size(); i++) {
            Integer multiId = multi.get(i);
            questionService.insertMultiRelation(examId, multiId,  k, exam.getMultiScore());
            k++;
        }
        for (int i = 0; i < judge.size(); i++) {
            Integer judgeId = judge.get(i);
            questionService.insertJudgeRelation(examId, judgeId,  k, exam.getJudgeScore());
            k++;
        }
        //questionService.selectByType
    }

    @Override
    public void delete(Long id) {
        examMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Exam exam) {
        examMapper.updateByPrimaryKey(exam);
    }

    @Override
    public Exam get(Long id) {
        return examMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Exam> listAll() {
        return examMapper.selectAll();
    }

    @Override
    public PageInfo<Exam> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        List<Exam> exams = examMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Exam>(exams);
    }
}
